<template>
  <div class="dashboard-container">
    <el-card class="tree-card">
      <!-- 用来一个行列布局 -->
      <treeTools :tree-node="company" @showdia="showdiafn" />
    </el-card>

    <el-tree :data="departs" :props="defaultProps">
      <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
      <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
      <treeTools slot-scope="{ data }" :flag="flag" :tree-node="data" @delDepts="getDepartments" @showdia="showdiafn"  @editDepts="editDepts"/>
    </el-tree>
    <!-- 新增所用的弹窗组件 -->
    <addDept ref="showdialog" @addDepts="fn" :showdialog.sync="showdialog" :tree-node="node">
</addDept>
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools.vue'
import addDept from '@/views/departments/components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'

export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      node: {},
      showdialog: false,
      // node: {},
      flag: true,
      company: { id: '',
        name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      departs: [
        // { name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        // { name: '行政部', manager: '刘备' },
        // { name: '人事部', manager: '孙权' }
      ],
        defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取组织架构数据的方法
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res, '-==-=-=123')
      this.departs = tranListToTreeData(res.depts, '')
    },
    // 接收 tools传递的数据
    showdiafn(node) {
      this.node = node
      this.showdialog = true
    },
    fn() {
      // this.showdialog = false
      this.getDepartments()
    },
    editDepts(node) {
      // 首先打开弹层
      this.showdialog = true
      this.node = node // 赋值操作的节点
      // 调用弹窗组件里的 获取当前部门详细信息的方法
      this.$refs.showdialog.getDepartDetail(node.id)
    }
  }

}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>

